Skip to content

Sync binary-search with the updated tutorial - #943

Merged
realpython-bot merged 2 commits into
masterfrom
maintenance/binary-search-python-20261113
Nov 13, 2026
Merged

realpython-bot merged 2 commits into
masterfrom
maintenance/binary-search-python-20261113

Conversation

@realpython-bot

Copy link
Copy Markdown
Collaborator

Companion change for the maintenance update of How to Do a Binary Search in Python.

What changed

  • search/binary.py: find_all_indices() tested the leftmost and rightmost indices for truthiness, so when the first match sat at index 0 it returned an empty set (find_all_indices(["apple", "apple", "banana"], "apple") returned set() instead of {0, 1}). It now compares both indices against None. The tutorial draft gets the same fix.

Links

Verification (Python 3.15.2)

  • find_all_indices() returns {0, 1}, {1, 2}, and set() for a leftmost match at index 0, a middle match, and a miss. find_all() returns {"apple"}.
  • benchmark.py runs the random, linear, and binary algorithms against a small names file.
  • download_imdb.py downloads the IMDb dataset and writes names.txt and sorted_names.txt (15.8M lines each).
  • ruff@0.14.1 format --check and ruff@0.14.1 check both pass on binary-search/.

Note: download_imdb.py uses parenthesized context managers, so it needs Python 3.9 or later. The benchmark.py docstring and the tutorial both say 3.7. I left both claims unchanged so a human can decide.

🤖 Generated with Claude Code

realpython-bot and others added 2 commits November 13, 2026 06:12
…Search in Python

find_all_indices() tested the leftmost and rightmost indices for
truthiness, so a match at index 0 returned an empty set. Compare them
against None instead, matching the updated tutorial.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The tutorial and benchmark.py state a Python 3.7 floor, but the
parenthesized context managers in download_imdb.py need 3.9. Nested
with statements restore the stated floor (vermin: 3.7) without changing
behavior.
@realpython-bot
realpython-bot merged commit fc2943d into master Nov 13, 2026
1 check passed
@realpython-bot
realpython-bot deleted the maintenance/binary-search-python-20261113 branch November 13, 2026 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant